package com.childenglish.controller;

import com.childenglish.service.LearningProgressService;
import com.childenglish.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/progress")
public class ProgressController {

    @Autowired
    private LearningProgressService progressService;

    /**
     * 获取学习报告
     */
    @GetMapping("/report/{userId}")
    public R getLearningReport(@PathVariable Long userId) {
        try {
            Map<String, Object> report = progressService.getLearningReport(userId);
            return R.ok("获取学习报告成功").data(report);
        } catch (Exception e) {
            return R.error("获取学习报告失败: " + e.getMessage());
        }
    }

    /**
     * 获取学习统计
     */
    @GetMapping("/statistics/{userId}")
    public R getLearningStatistics(@PathVariable Long userId) {
        try {
            Map<String, Object> stats = progressService.getLearningStatistics(userId);
            return R.ok("获取学习统计成功").data(stats);
        } catch (Exception e) {
            return R.error("获取学习统计失败: " + e.getMessage());
        }
    }

    /**
     * 记录学习进度
     */
    @PostMapping("/record")
    public R recordProgress(@RequestBody Map<String, Object> progressData) {
        try {
            Long userId = Long.valueOf(progressData.get("userId").toString());
            String module = (String) progressData.get("module");
            int progress = Integer.parseInt(progressData.get("progress").toString());
            int score = Integer.parseInt(progressData.get("score").toString());

            progressService.recordProgress(userId, module, progress, score);
            return R.ok("学习进度记录成功");

        } catch (Exception e) {
            return R.error("记录学习进度失败: " + e.getMessage());
        }
    }
}